package com.token.zhuishushenqi.community.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.token.zhuishushenqi.R;
import com.token.zhuishushenqi.community.entity.DynamicHotJson;
import com.token.zhuishushenqi.community.util.CommunityHttpUtils;
import com.token.zhuishushenqi.other.asyn.ZhuishuTask;
import com.token.zhuishushenqi.other.util.HttpUtils;

import java.util.List;

/**
 *
 * 热门fragment的适配器
 * Created by pengjianhuan on 2016/11/8 0008.
 */

public class HotListViewAdapter extends BaseAdapter {
    private List<DynamicHotJson> hotJsonList;
    private Context context;

    private String hotAvatarUrl;

    Bitmap bitmap;

    private ZhuishuTask zhuishuTaskImage;
    public HotListViewAdapter(Context context,  List<DynamicHotJson> hotJsonList) {
        this.context = context;

        this.hotJsonList = hotJsonList;
    }

    @Override
    public int getCount() {
        return hotJsonList == null? 0:hotJsonList.size();
    }

    @Override
    public Object getItem(int position) {
        return hotJsonList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {

            convertView = View.inflate(context,R.layout.item_hot_listview,null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

       /* hotAvatarUrl = hotJsonList.get(position).getAvatar();

        if (hotAvatarUrl!=null){
            downloadImage();
        }
        viewHolder.avatar.setImageBitmap(bitmap);
        viewHolder.nickname.setText(hotJsonList.get(position).getNickname());
        viewHolder.time.setText(hotJsonList.get(position).getHotAt());
        viewHolder.content.setText(hotJsonList.get(position).getContent());
        viewHolder.title.setText(hotJsonList.get(position).getTitle());*/
        return convertView ;
    }

    class ViewHolder{
        TextView nickname,time,title,content;
        ImageView avatar;

        public ViewHolder(View view){
            nickname = (TextView) view.findViewById(R.id.hot_lvitem_nickname);
            time = (TextView) view.findViewById(R.id.hot_lvitem_time);
            content = (TextView) view.findViewById(R.id.hot_lvitem_content);
            title = (TextView) view.findViewById(R.id.hot_lvitem_title);
            avatar = (ImageView) view.findViewById(R.id.hot_lvitem_iv);

        }
    }

    public void downloadImage(){
        zhuishuTaskImage = new ZhuishuTask(new ZhuishuTask.IDownloadImage() {
            @Override
            public Bitmap downloadBitmap() {

                return HttpUtils.getBitmap(CommunityHttpUtils.HOT_URL_AVATAR + hotAvatarUrl);
            }
        }, new ZhuishuTask.IRequestCallback() {
            @Override
            public void onSuccess(Object object) {

                bitmap = (Bitmap) object;

                Toast.makeText(context,"下载图片成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError() {
                Toast.makeText(context,"下载图片失败",Toast.LENGTH_SHORT).show();
            }
        });

        zhuishuTaskImage.execute();

    }

    public void cancel(){

        if (zhuishuTaskImage!=null) {
            zhuishuTaskImage.cancel(true);
            zhuishuTaskImage = null;
        }
    }

}
